/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.service;

import com.seari.commonutils.redis.JedisUtilProxy;
import java.util.Set;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 *
 * @author Rainbow
 */
@Service("RedisService")
public class RedisServiceImpl implements RedisService
{

    @Override
    public boolean isHashExist(String set, String hash)
    {
        boolean result = true;
        Jedis jedis = null;
        try
        {
            jedis = JedisUtilProxy.getJedis();
            result = jedis.sismember(set, hash);
        } catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            JedisUtilProxy.close(jedis);
        }
        return result;
    }

    @Override
    public void insertHashToSet(String set, String... hash)
    {
        Jedis jedis = null;
        try
        {
            jedis = JedisUtilProxy.getJedis();
            jedis.sadd(set, hash);
        } catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            JedisUtilProxy.close(jedis);
        }
    }

    @Override
    public void insertTitleToHash(String hash, String key, String value)
    {
        Jedis jedis = null;
        try
        {
            jedis = JedisUtilProxy.getJedis();
            jedis.hset(hash, key, value);
        } catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            JedisUtilProxy.close(jedis);
        }
    }

    @Override
    public Set<String> getHashKeys(String hash)
    {
        Jedis jedis = null;
        Set<String> keys = null;
        try
        {
            jedis = JedisUtilProxy.getJedis();
            keys = jedis.hkeys(hash);
        } catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            JedisUtilProxy.close(jedis);
        }
        return keys;
    }

    @Override
    public String getHashValue(String hash, String key)
    {
        Jedis jedis = null;
        String result = "";
        try
        {
            jedis = JedisUtilProxy.getJedis();
            result = jedis.hget(hash, key);
        } catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            JedisUtilProxy.close(jedis);
        }
        return result;
    }
    
}
